package com.dxj1992.utils.UserAsset;

import com.dxj1992.exception.CommonException;
import com.dxj1992.utils.Arith;

import java.math.BigDecimal;

public class Expend extends Asset {

    @Override
    public void beforeUser() {
        this.setAmount(BigDecimal.valueOf(this.getAmount()).negate().doubleValue());
        this.setType(Asset.TYPE_EXPEND);
    }

    @Override
    public void updateUserAsset() {

        // 取绝对值
        double amount = BigDecimal.valueOf(this.getAmount()).abs().doubleValue();
        double balance = this.getUserAsset().getBalance().doubleValue();

        // 检测余额是否够本次支出
        if (Arith.sub(balance, amount) < 0) {
            throw new CommonException("您的余额不足");
        }

        // 更新卡平台余额、入库金额
        this.getUserAsset().setBalance(BigDecimal.valueOf(Arith.sub(balance, amount)));
        this.getUserAsset().setTotalExpend(BigDecimal.valueOf(Arith.add(this.getUserAsset().getTotalExpend().doubleValue(), amount)));
        this.getIUserAssetService().updateById(this.getUserAsset());
    }

}
